/* 消息封包 */
Package = function (uin, cmd, seq, session, bd) {
	this.head = {
		uin : uin,
		cmd : cmd,
		seq : seq,
		session : session
	};
	this.body = bd;
};

Package.TIMEOUT = 30000;
Package.RETRYCNT = 3;

Package.prototype = {
	head : {
		uin : 0,
		cmd : 0,
		seq : 0,
		session : ""
	},
	body : "",
	dwSendTime : 0,
	dwTimeout : 0,
	dwRetryCnt : 1,
	sfn : null,
	sscope : null,
	ffn : null,
	fscope : null,
	option : null,
	setOption : function(opt) {
		this.option = opt;
		this.dwTimeout = opt.timeout || Package.TIMEOUT;
		this.dwRetryCnt = opt.retrycnt || Package.RETRYCNT
	},
	setSendTime : function(sentTime) {
		this.dwSendTime = sentTime;
	},
	toString : function() {
		var obj = { 
			'uin':this.head.uin, 
			'cmd':this.head.cmd, 
			'seq':this.head.seq, 
			'session':this.head.session, 
			'body':this.body
		};
		return YAHOO.lang.JSON.stringify(obj);
	}
};

Transport = {
	cs_seq : 0,
	RELAY_TIME : 1000,
	relay_timer : null,
	chatClient : null,
	package_queue : {},
	ready_queue : [],
	init : function(cc) {
		//alert("initial the transport!");
		this.chatClient = cc;
	},
	pushPackage : function(pkg) {
		this.package_queue[pkg.head.seq] = pkg
	},
	findPackage : function(seq) {
		return this.package_queue[seq] || null
	},
	removePackage : function(seq) {
		var pkg = this.package_queue[seq] || null;
		if (pkg && pkg.head.cmd == "30") {
			YAHOO.log("[" + seq + "]=" + pkg, "warn")
		}
		delete this.package_queue[seq];
		return pkg;
	},
	peekPackage : function() {
		var pkg = null;
		for ( var seq in this.package_queue) {
			pkg = this.package_queue[seq];
			break;
		}
		return pkg;
	},
	shiftPackage : function() {
		var pkg = null;
		for ( var seq in this.package_queue) {
			pkg = this.package_queue[seq];
			delete this.package_queue[seq];
			break
		}
		return pkg;
	},
	sendPackage : function(pkg) {
		this.relaySendPackage(pkg);
	},
	relaySendPackage : function(pkg) {
		if (!pkg || !pkg.head) {
			return
		}

		this.ready_queue.push(pkg);
		if (this.relay_timer) {
			clearTimeout(this.relay_timer);
			this.relay_timer = null
		}
		var me = this;
		this.relay_timer = setTimeout(function() {
			me.sendMultiPackage()
		}, me.RELAY_TIME);
	},
	sendMultiPackage : function() {
		this.relay_timer = null;
		var m = this.ready_queue;
		if (m.length <= 0) {
			return
		}
		
		var pkgs = [];
		for ( var l = 0; l < m.length; ++l) {
			m[l].setSendTime(new Date().getTime());
			if (m[l] && m[l].head.cmd != "00") {
				//如果不为ping包则缓存以进行act
				this.package_queue[m[l].head.seq] = m[l];
			}
			try {
				pkgs.push(YAHOO.lang.JSON.parse(m[l].toString()));
			} catch(e) {alert(e)}
		}
		var n = "";
		for ( var seq in this.package_queue) {
			n += this.package_queue[seq];
		}

		var j;
		try {
			j = YAHOO.lang.JSON.stringify(pkgs);
		} catch(e) {alert(e)}

		YAHOO.log("sendMultiPackage:" + j);
		//alert(this.chatClient.http_callback.onSuccess);
		this.ready_queue = [];
		YAHOO.util.Connect.asyncRequest("POST", this.chatClient.chat_proxy_uri,  
			{
				timeout: this.chatClient.http_callback.timeout,
				success:this.chatClient.http_callback.onSuccess,
				failure: this.chatClient.http_callback.onFailure,
				scope: this.chatClient.http_callback.scope
			}, j);
	},
	removeAllPackage : function() {
		this.package_queue = {};
		this.ready_queue = []
	}
};